/*
 * swap.cpp
 *
 *  Created on: Aug 16, 2012
 *      Author: jh238d
 */
#include "LinkedListNode.h"
#include <iostream>
using namespace std;

void reverseLinkedList(Node *&head, int i, Node *tail, Node *prev){
	if(!head) return;
	Node *curr = head;
	while(i>0){
		Node *tmp=curr->next;
		curr->next=prev;
		prev=curr;
		curr=tmp;
	}
	head->next = tail;
	head=prev;
}

void swap_list(Node *&head, int n){
	Node *curr=head;
	Node *prev = NULL;
	while(curr!=NULL){
		int i=n;
		Node *start = curr;
		while(i>0&&curr!=NULL){
			curr=curr->next;
			i--;
		}
		reverseLinkedList(start, n-i, curr, prev);
		prev->next = start;
		while(prev->next!=curr)
			prev=prev->next;

	}
}



